package core;

import java.util.ArrayList;
import org.apache.log4j.Logger;

/**
 * Applies the changes to the database
 *
 * @author Sakis Vtdk <sakis.vtdk@gmail.com>
 */
public class ChangesApplier 
{
        private void ChangesApplier()
        {

        }

		/**
		 * Gets the singleton instance of the {@link ChangesApplier}
		 * 
		 * @return The instance of the ChangesApplier
		 */
		public ChangesApplier getChangesApplier()
		{
			if(_instance == null)
			{
				_instance = new ChangesApplier();
			}

			return _instance;
		}


		/**
		 * Applies the given {@link Change} to the {@link Object}
		 * 
		 * @param object The object to be changed
		 * @param change The change to be performed
		 */
		public void applyChange(Object object, Change change)
		{
			//specify the type of the object
			if(object.getClass().equals(TableColumn.class))
			{
				TableColumn tblCol = (TableColumn) object;

				if(change.getType().equals(Change.Type.UPDATE.toString()))
				{
					this.updateColumn(tblCol, change);
				}
			}
			else if(object.getClass().equals(DatabaseTable.class))
			{
				DatabaseTable dbTable = (DatabaseTable) object;
			}
			
			
		}

        /**
         * Updates a {@link DatabaseTable} according to the {@link Change} object 
         * passed as argument
         * 
         * @param change A {@link Change} containing the update to be made to the table
         */
        private void updateTable(Change change)
        {
                //TODO: Add support for modifying tables
        }

        /**
         * Updates a {@link TableColumn} according to the {@link Change} object
         * passed as argument
         * 
		 * @param column The column to be updated
         * @param change A {@link Change} containing the update to be made to the column
         */
        private void updateColumn(TableColumn column, Change change)
        {
                String newValue = change.getNewValue();

                if(change.getWhatChanged().equals(Change.WhatChanged.NAME.toString()))
                {
					column.setName(newValue);
                }
                else if(change.getWhatChanged().equals(Change.WhatChanged.DATA_TYPE.toString()))
                {
					column.setDataType(newValue);
                }
        }

		/**
		 * Singleton instance
		 */
		private static ChangesApplier _instance = null;

        /**
         * The list of the changes to be applied on the database
         */
        private ArrayList<Change> _changes = new ArrayList();

        /**
         * Logger instance
         */
        private static Logger log = Logger.getLogger(ChangesApplier.class);
}